package com.copa.module.cart.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.copa.domain.ResponseDto;
import com.copa.module.cart.service.GoodsManage;
import com.copa.module.goods.domain.dto.GoodsAttributeDto;
import com.copa.module.goods.domain.dto.GoodsProductDto;
import com.copa.module.goods.domain.dto.GoodsSpecDto;
import com.copa.module.goods.domain.entity.GoodsProductEntity;
import com.copa.module.goods.mapper.GoodsAttributeDao;
import com.copa.module.goods.mapper.GoodsProductDao;
import com.copa.module.goods.mapper.GoodsSpecificationsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author copa
 * @createDate 2021-01-22 15:18
 * @function
 */
@Service
public class GoodsManageImpl implements GoodsManage {

    @Autowired
    private GoodsProductDao goodsProductDao;

    @Autowired
    private GoodsAttributeDao goodsAttributeDao;

    @Autowired
    private GoodsSpecificationsDao specificationsDao;

    @Override
    public ResponseDto<List<GoodsProductDto>> findGoodsSpuByGoodsId(Long goodsId) {
        List<GoodsProductDto> goodsProductDtos = goodsProductDao.findGoodsSpuByGoodsId(goodsId);
        if (ObjectUtil.isNotNull(goodsProductDtos) && ObjectUtil.length(goodsProductDtos) > 0) {
            return ResponseDto.success(goodsProductDtos);
        }
        return ResponseDto.errorMsg("没有该商品的spu信息");
    }

    @Override
    public ResponseDto<List<GoodsAttributeDto>> findGoodsAttributeByGoodsId(Long goodsId) {
        List<GoodsAttributeDto> goodsAttributeDtos =
                goodsAttributeDao.findGoodsAttributeByGoodsId(goodsId);
        if (ObjectUtil.isNotNull(goodsAttributeDtos)
                && ObjectUtil.length(goodsAttributeDtos) > 0) {
            return ResponseDto.success(goodsAttributeDtos);
        }
        return ResponseDto.errorMsg("没有该商品的规格信息");
    }

    @Override
    public ResponseDto<List<GoodsSpecDto>> findGoodsSpecByGoodsId(Long goodsId) {
        List<GoodsSpecDto> goodsAttributeDtos =
                specificationsDao.findGoodsSpecByGoodsId(goodsId);
        if (ObjectUtil.isNotNull(goodsAttributeDtos)
                && ObjectUtil.length(goodsAttributeDtos) > 0) {
            return ResponseDto.success(goodsAttributeDtos);
        }
        return ResponseDto.errorMsg("没有该商品的spu信息");
    }

    @Override
    public BigDecimal calTotalMoney(List<Long> cartIds) {
        return goodsProductDao.calTotalMoney(cartIds);
    }

    @Override
    public GoodsProductEntity findGoodsSpuByProductId(Long productId) {
        GoodsProductEntity productEntity = goodsProductDao.selectById(productId);
        return productEntity;
    }
}
